package com.aiwei.tdjk.controller.sys;

import com.aiwei.tdjk.bean.ApiResult;
import com.aiwei.tdjk.bean.SafeKey;
import com.aiwei.tdjk.entity.User;
import com.aiwei.tdjk.service.PasswordService;
import com.aiwei.tdjk.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.*;
import java.util.Date;
import java.util.UUID;

/**
 * Controller - 会员中心 - 密码
 * 
 */
@Controller
@RequestMapping("/user/password")
public class PasswordController {

	@Autowired
	private UserService userService;


	@Autowired
	private PasswordService passwordService;


	/**
	 * 找回密码
	 */
//	@RequestMapping(value = "/find", method = RequestMethod.GET)
//	@ResponseBody
//	public String find(Model model) {
//
//		SafeKey safeKey = new SafeKey();
//		safeKey.setValue("123456");
//		safeKey.setExpire(new Date(2019,12,12));
//
//		model.addAttribute("captchaId", UUID.randomUUID().toString());
//		emailSender.sendFindPasswordMail("yanfeng106@163.com", "订单到期通知" + "", safeKey);
//		return "/shop/newPassword/find";
//	}

	/**
	 * 找回密码提交(邮箱验证之后才能够发送-优化)
	 */
	@RequestMapping(value = "/find", method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<String> find(String captchaId, String captcha, String username, String email) {

		ApiResult apiResult = ApiResult.makeSucessResult();
		if (/*StringUtils.isEmpty(username) ||*/ StringUtils.isEmpty(email)) {
			return ApiResult.makeServerFailed("邮箱不存在");
		}
		User member = userService.findByUsernameOrMobileOrEmailOrEmployeeId(email);
		if (member == null) {
			return ApiResult.makeServerFailed("shop.newPassword.memberNotExist");
		}
		if (!member.getEmail().equalsIgnoreCase(email)) {
			return ApiResult.makeServerFailed("shop.newPassword.invalidEmail");
		}

//		Setting setting = SettingUtils.get();
		SafeKey safeKey = new SafeKey();
		safeKey.setValue(UUID.randomUUID().toString() + DigestUtils.md5Hex(RandomStringUtils.randomAlphabetic(30)));

		LocalTime localTime = LocalTime.now();
		LocalDate localDate = LocalDate.now();
		LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
		LocalDateTime.now();
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		java.util.Date date = Date.from(instant);

		safeKey.setExpire( DateUtils.addMinutes(date, 60));
		member.setSafekeyDate(safeKey.getExpire());
		member.setSafekeyValue(safeKey.getValue());
		userService.update(member);
//		mailService.sendFindPasswordMail(member.getEmail(), member.getUsername(), safeKey);
		return apiResult;
	}


	/**
	 * 重置密码提交
	 */
	@RequestMapping(value = "/reset", method = RequestMethod.GET)
	public String reset(Model model, String username,String key) {
		User user = userService.findByUsername(username);
		model.addAttribute("user",user);
		model.addAttribute("key",key);
		return "/admin/password/reset";
	}

	/**
	 * 重置密码提交
	 */
	@RequestMapping(value = "/reset", method = RequestMethod.POST)
	public @ResponseBody
	ApiResult<String> reset(String username, String newPassword, String key) {

		ApiResult apiResult = ApiResult.makeSucessResult();

		User member = userService.findByUsername(username);
		if (member == null) {
			return ApiResult.makeServerFailed("shop.newPassword.memberNotExist");
		}
//		Setting setting = SettingUtils.get();
		if (newPassword.length() < 6 || newPassword.length() > 20) {
			return ApiResult.makeServerFailed("shop.newPassword.invalidPassword");
		}
		if (member.getSafekeyValue() == null || !member.getSafekeyValue().equals(key)) {
			return ApiResult.makeServerFailed("安全码错误，请重新发送邮件验证！");
		}
		Date ExpiredDate = member.getSafekeyDate();
		if (ExpiredDate != null && new Date().after(ExpiredDate)) {
			return ApiResult.makeServerFailed("shop.newPassword.hasExpired");
		}
		member.setPassword(passwordService.encryptPassword(member.getUsername(), newPassword, member.getSalt()));

		member.setSafekeyDate(new Date());
		member.setSafekeyValue(null);
		userService.update(member);
		return apiResult;
	}
	


}